﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using ICalendar.ValueTypes;
using System.Linq;

namespace ICalendar.CalendarProperties
{
    [XmlRoot("recurrence-id")]
    [Serializable]
    public class RecurrenceId : HybridDateTimeBase
    {

        // <summary>
        /// To specify the effective range of recurrence instances from the instance specified by the recurrence identifier specified by the property.
        /// </summary>
        public Range? Range
        {
            get;
            set;
        }

        public RecurrenceId(DateTime value)
            : base(value)
        {
        }

        public static implicit operator RecurrenceId(DateTime value)
        {
            return new RecurrenceId(value);
        }

        protected override void SetParametersFromXParameters(XParameterCollection xparams)
        {
            if (xparams == null)
                return;

            base.SetParametersFromXParameters(xparams);       
            string range = xparams.GetValue("RANGE");
            this.Range = string.IsNullOrEmpty(range) ? null : (Range?)Enum.Parse(typeof(Range), range, true);
        }

        protected override XParameterCollection GetXParametersFromParameters()
        {

            var xparams = base.GetXParametersFromParameters() ?? new XParameterCollection();
            xparams.Add("RANGE", this.Range.ToString().ToUpperInvariant());
            return xparams;
        }


          protected override string[] GetXReservedParametersNames()
        {
              var names = base.GetXReservedParametersNames();
              var nameList = names!=null ? names.ToList() : new List<string>();

              nameList.Add("RANGE");

              return nameList.ToArray();
        }
   
    }

}
